package com.rock.code.base.service.impl;

import com.rock.code.base.dao.impl.BaseDaoImpl;
import com.rock.code.base.domain.Constants;
import com.rock.code.common.pager.Page;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

/**
 * The type Base service.
 *
 * @param <T> the type parameter
 * @author kongqiang
 * @date 2016.12.21
 */
public class BaseServiceImpl<T> {

    private String NAMESPACE = ((Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]).getName();

    /**
     * The Base dao.
     */
    @Resource
    private BaseDaoImpl baseDaoImpl;

    /**
     * Gets db column by property.
     *
     * @param property    the property
     * @param resultMapId the result map id
     * @return the db column by property
     * @author kongqiang
     * @date 2016.12.21
     */
    public String getDbColumnByProperty(String property, String resultMapId) throws Exception {
        return baseDaoImpl.getDbColumnByProperty(property, NAMESPACE + "." + resultMapId);
    }

    /**
     * Gets order by.
     *
     * @param page the page
     * @return the order by
     * @throws Exception the exception
     * @author kongqiang
     * @date 2016.12.29
     */
    public String getOrderBy(Page page) throws Exception {
        String orderBy = "";
        String dbColumn = this.getDbColumnByProperty(page.getSidx(), Constants.RESULT_MAP_ID);
        if (!dbColumn.equals("")) {
            orderBy = dbColumn + " " + page.getSord();
        }
        return orderBy;
    }

    /**
     * Gets order by.
     *
     * @param page the page
     * @param resultMapId the resultMapId
     * @return the order by
     * @throws Exception the exception
     * @author kongqiang
     * @date 2016.12.29
     */
    public String getOrderBy(Page page, String resultMapId) throws Exception {
        String orderBy = "";
        String dbColumn = this.getDbColumnByProperty(page.getSidx(), resultMapId);
        if (!dbColumn.equals("")) {
            orderBy = dbColumn + " " + page.getSord();
        }
        return orderBy;
    }

    /**
     * Query object.
     *
     * @param statement the statement
     * @param para      the para
     * @return the object
     * @throws Exception the exception
     * @author kongqiang
     * @date 2017.03.23
     */
    public Object queryObject(String statement, Map<String, Object> para) throws Exception {
        return baseDaoImpl.queryObject(NAMESPACE + "." + statement, para);
    }

    /**
     * Query object list.
     *
     * @param statement the statement
     * @param para      the para
     * @return the list
     * @throws Exception the exception
     * @author kongqiang
     * @date 2017.03.23
     */
    public List<Object> queryObjectList(String statement, Map<String, Object> para) throws Exception {
        return baseDaoImpl.queryObjectList(NAMESPACE + "." + statement, para);
    }

}
